package com.tjsiqi.service;

import com.tjsiqi.Vo.*;
import com.tjsiqi.beans.PageQuery;
import com.tjsiqi.beans.PageResult;
import com.tjsiqi.dao.JblSendTruckOUTMapper;
import com.tjsiqi.dao.WebusercontrolMapper;
import com.tjsiqi.dto.SendCountDto;
import com.tjsiqi.dto.SendCountDtoM;
import com.tjsiqi.exception.ParamException;
import com.tjsiqi.param.SendCountParam;
import com.tjsiqi.param.SendCountParamM;
import com.tjsiqi.param.WeighCountParam;
import com.tjsiqi.util.DataHandleUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SysSendCountService {
    @Autowired
    private JblSendTruckOUTMapper jblSendTruckOUTMapper;

    @Autowired
    private WebusercontrolMapper webusercontrolMapper;

    static String[] levels = {"C10","C15","C20","C25","C30","C35","C40","C45","C50","C55","C60","Other"};

    static List<String> list = Arrays.asList(levels);

    static String[] proLine = {"1","2","3","4","5","Other"};

    static List<String> linelist = Arrays.asList(proLine);

//    static List<SendProLineVo> staticsendProLineVoList = new ArrayList<>();
//
//    static {
//        for(int i = 1 ;i <= 5; i++){
//            staticsendProLineVoList.add(new  SendProLineVo(""+i,0.0,"0%"));
//        }
//    }
//
//    static List<SendLevelVo> staticsendLevelVoList = new ArrayList<>();
//
//    static {
//        for(int i = 10 ;i <= 60; i+=5){
//             staticsendLevelVoList.add(new  SendLevelVo("C"+i,0.0,0));
//        }
//    }

//    static  SendLevelVo staticSendLevelVo = new SendLevelVo("Other",0.0,0);


    //按客户
    public PageResult<SendCustomerVo> getSendCountByCustomer(SendCountParam sendCountParam, PageQuery page) {

        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        int count = jblSendTruckOUTMapper.getSendCountByCustomer(sendCountDto);

        if (count > 0){
            List<SendCustomerVo> sendCustomerVoList = jblSendTruckOUTMapper.getPageListBysendCountDto(sendCountDto, page);
            return PageResult.<SendCustomerVo>builder().total(count).data(sendCustomerVoList).build();
        }
        return PageResult.<SendCustomerVo>builder().build();
    }

    //按车牌
    public PageResult<SendTruckVo> getSendCountByTruck(SendCountParam sendCountParam, PageQuery page) {

        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        int count = jblSendTruckOUTMapper.getSendCountByTruck(sendCountDto);

        System.out.println("count"+count);
        if (count > 0){
            List<SendTruckVo> sendTruckVoList = jblSendTruckOUTMapper.getPageListByTruck(sendCountDto, page);
            return PageResult.<SendTruckVo>builder().total(count).data(sendTruckVoList).build();
        }
        return PageResult.<SendTruckVo>builder().build();
    }

    //按业务员
    public PageResult<SendSenderVo> getSendCountBySender(SendCountParam sendCountParam, PageQuery page) {

        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        int count = jblSendTruckOUTMapper.getSendCountBySender(sendCountDto);
        if (count > 0){
            List<SendSenderVo> sendSenderVoList = jblSendTruckOUTMapper.getPageListBySender(sendCountDto, page);
            return PageResult.<SendSenderVo>builder().total(count).data(sendSenderVoList).build();
        }
        return PageResult.<SendSenderVo>builder().build();

    }

    //按操作员

    public PageResult<SendOperatorVo> getSendCountByOperator(SendCountParam sendCountParam, PageQuery page) {

        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        int count = jblSendTruckOUTMapper.getSendCountByOperator(sendCountDto);
        if (count > 0){
            List<SendOperatorVo> sendOperatorVoList = jblSendTruckOUTMapper.getPageListByOperator(sendCountDto, page);
            return PageResult.<SendOperatorVo>builder().total(count).data(sendOperatorVoList).build();
        }
        return PageResult.<SendOperatorVo>builder().build();

    }

    private static SendCountDto getSendCDto(SendCountParam sendCountParam){
        SendCountDto sendCountDto = new SendCountDto();
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (StringUtils.isNotBlank(sendCountParam.getFromTime())) {
                sendCountDto.setFromTime(dateFormat.parse(sendCountParam.getFromTime()+" 00:00:00"));
            }else{
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR_OF_DAY, 0);
                // 分
                cal.set(Calendar.MINUTE, 0);
                // 秒
                cal.set(Calendar.SECOND, 0);
                // 毫秒
                cal.set(Calendar.MILLISECOND, 0);
//                cal.add(Calendar.DATE, -7);
                //设置开始时间
                sendCountDto.setFromTime(cal.getTime());
            }
            //设置结束时间
            if (StringUtils.isNotBlank(sendCountParam.getToTime())) {
                sendCountDto.setToTime(dateFormat.parse(sendCountParam.getToTime()+" 23:59:59"));
            }else{
                Calendar calTo = Calendar.getInstance();
                calTo.set(Calendar.HOUR_OF_DAY, 23);
                // 分
                calTo.set(Calendar.MINUTE, 59);
                // 秒
                calTo.set(Calendar.SECOND, 59);
                // 毫秒
                calTo.set(Calendar.MILLISECOND, 0);
                sendCountDto.setToTime(calTo.getTime());
            }
        } catch (Exception e) {
            throw new ParamException("传入的日期格式有问题，正确格式为：yyyy-MM-dd HH:mm:ss");
        }

        sendCountDto.setJbzCode(sendCountParam.getJbzCode());

        return sendCountDto;
    }

    //按等级
    public PageResult<SendLevelVo> getSendCountByLevel(SendCountParam sendCountParam) {

        //建立按等级查询的对象空集合
        List<SendLevelVo> staticsendLevelVoList = new ArrayList<>();

        //添加等级分类赋初值为0.0
        for(int i = 10 ;i <= 60; i+=5){
            staticsendLevelVoList.add(new  SendLevelVo("C"+i,0.0,0));
        }

        SendCountDto sendCountDto = getSendCDto(sendCountParam);
        int count = jblSendTruckOUTMapper.getSendCountByLevel(sendCountDto);
        System.out.println("测试count大于0执行----"+(count>0));
        if (count > 0){
            System.out.println("测试count大于0执行--2--"+(count>0));
            //从数据库中查询出等级对象集合
            List<SendLevelVo> sendLevelVoList = jblSendTruckOUTMapper.getPageListByLevel(sendCountDto);
            SendLevelVo staticSendLevelVo = new SendLevelVo("Other",0.0,0);
            //判断是否包含其它等级对象，不包含增加一个其它对象保存不属于等级内的数据
            if(!staticsendLevelVoList.get(staticsendLevelVoList.size()-1).getConlevel().equals("Other")) {
                staticsendLevelVoList.add(staticSendLevelVo);
            }else{
                staticsendLevelVoList.remove(staticsendLevelVoList.size()-1);
                staticsendLevelVoList.add(staticSendLevelVo);
           }

            for(int i= 0;i<sendLevelVoList.size();i++){
                SendLevelVo sendLevelVo = sendLevelVoList.get(i);
                if(list.indexOf(sendLevelVo.getConlevel())>-1){
                    int index = list.indexOf(sendLevelVo.getConlevel());
                    Collections.replaceAll(staticsendLevelVoList,staticsendLevelVoList.get(index),sendLevelVo);
                }else {
                    staticSendLevelVo.setConnum(staticSendLevelVo.getConnum()+sendLevelVo.getConnum());
                    staticSendLevelVo.setSumsendnub(staticSendLevelVo.getSumsendnub()+sendLevelVo.getSumsendnub());
                    Collections.replaceAll(staticsendLevelVoList,staticSendLevelVo,staticSendLevelVo);
                }

            }

            return PageResult.<SendLevelVo>builder().total(count).data(staticsendLevelVoList).build();
        }


        return PageResult.<SendLevelVo>builder().build();
    }

    //按生产线
    public PageResult<SendProLineVo> getSendCountByProLine(SendCountParam sendCountParam) {

        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        List<SendProLineVo> staticsendProLineVoList = new ArrayList<>();

        for(int i = 1 ;i <= 5; i++){
            staticsendProLineVoList.add(new  SendProLineVo(""+i,0.0,"0%"));
        }

        Double count = jblSendTruckOUTMapper.getSendCountByProLine(sendCountDto);
        if(count == null) count = 0.0;
        if (count > 0){
            List<SendProLineVo> sendProLineVoList = jblSendTruckOUTMapper.getPageListByProLine(sendCountDto);
            SendProLineVo staticSendProLineVo = new SendProLineVo("Other",0.0,"0%");
            //
            if(!staticsendProLineVoList.get(staticsendProLineVoList.size()-1).getProductlineid().equals("Other")) {
                staticsendProLineVoList.add(staticSendProLineVo);
            }else{
                staticsendProLineVoList.remove(staticsendProLineVoList.size()-1);
                staticsendProLineVoList.add(staticSendProLineVo);
            }

            for(int i= 0;i<sendProLineVoList.size();i++){
                SendProLineVo sendProLineVo = sendProLineVoList.get(i);
                //
               if(linelist.indexOf(sendProLineVo.getProductlineid())>-1){
                    int index = linelist.indexOf(sendProLineVo.getProductlineid());
                    sendProLineVo.setConnumPersent(DataHandleUtil.division(sendProLineVo.getConnum(),count));
                    Collections.replaceAll(staticsendProLineVoList,staticsendProLineVoList.get(index),sendProLineVo);
                }else {
                    staticSendProLineVo.setConnum(staticSendProLineVo.getConnum()+sendProLineVo.getConnum());
                    staticSendProLineVo.setConnumPersent(DataHandleUtil.division(staticSendProLineVo.getConnum(),count));
                    Collections.replaceAll(staticsendProLineVoList,staticSendProLineVo,staticSendProLineVo);
                }

            }

            return PageResult.<SendProLineVo>builder().total(count.intValue()).data(staticsendProLineVoList).build();
        }


        return PageResult.<SendProLineVo>builder().build();

    }

    //按施工单位
    public PageResult<SendProUnitVo> getSendCountByProUnit(SendCountParam sendCountParam, PageQuery page) {

        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        int count = jblSendTruckOUTMapper.getSendCountByProUnit(sendCountDto);

        if (count > 0){
            List<SendProUnitVo> sendProUnitVoList = jblSendTruckOUTMapper.getPageListByProUnit(sendCountDto, page);
            return PageResult.<SendProUnitVo>builder().total(count).data(sendProUnitVoList).build();
        }
        return PageResult.<SendProUnitVo>builder().build();
    }

    //按现场联系人
    public PageResult<SendLinkManVo> getSendCountByLinkMan(SendCountParam sendCountParam, PageQuery page) {

        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        int count = jblSendTruckOUTMapper.getSendCountByLinkMan(sendCountDto);

        if (count > 0){
            List<SendLinkManVo> sendLinkManVoList = jblSendTruckOUTMapper.getPageListByLinkMan(sendCountDto, page);
            for(SendLinkManVo sendLinkManVo:sendLinkManVoList){
                System.out.println("sendLinkManVo"+sendLinkManVo);
            }
            return PageResult.<SendLinkManVo>builder().total(count).data(sendLinkManVoList).build();
        }
        return PageResult.<SendLinkManVo>builder().build();
    }

    //按工程名称
    public String getSendCountByPro(SendCountParam sendCountParam, String proUnit) {

        StringBuilder ProList = new StringBuilder();
        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        List<String> sendProList = jblSendTruckOUTMapper.getPageListByPro(sendCountDto, proUnit);

        for(int i= 0;i<sendProList.size();i++){
            ProList.append("("+(i+1)+")"+"."+sendProList.get(i)+"   ");
        }

        return ProList.toString();
    }

    //联系电话
    public String getSendCountByProLinkMan(SendCountParam sendCountParam, String proLinkMan) {

        StringBuilder ProLinkManList = new StringBuilder();
        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        List<String> sendProLinkManList = jblSendTruckOUTMapper.getPageListByProLinkMan(sendCountDto, proLinkMan);

        for(int i= 0;i<sendProLinkManList.size();i++){
            ProLinkManList.append("("+(i+1)+")"+"."+sendProLinkManList.get(i)+"   ");
        }

        return ProLinkManList.toString();
    }


    public PageResult<SendCustomerVoM> getSendCountByCustomerM(SendCountParamM sendCountParamM, PageQuery page) {
        SendCountDtoM sendCountDtoM = getSendCDtoM(sendCountParamM);
        System.out.println("接收管理员查询参数"+sendCountDtoM);
        int count = jblSendTruckOUTMapper.getSendCountByCustomerM(sendCountDtoM);

        if (count > 0){
            List<SendCustomerVoM> sendCustomerVoMList = jblSendTruckOUTMapper.getPageListByCustomerM(sendCountDtoM, page);
            return PageResult.<SendCustomerVoM>builder().total(count).data(sendCustomerVoMList).build();
        }
        return PageResult.<SendCustomerVoM>builder().build();

    }


    //初始化管理员查询参数
    private static SendCountDtoM getSendCDtoM(SendCountParamM sendCountParamM){
        SendCountDtoM sendCountDtoM = new SendCountDtoM();

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (StringUtils.isNotBlank(sendCountParamM.getFromTime())) {
                sendCountDtoM.setFromTime(dateFormat.parse(sendCountParamM.getFromTime()+" 00:00:00"));
            }else{
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR_OF_DAY, 0);
                // 分
                cal.set(Calendar.MINUTE, 0);
                // 秒
                cal.set(Calendar.SECOND, 0);
                // 毫秒
                cal.set(Calendar.MILLISECOND, 0);
//                cal.add(Calendar.DATE, -7);
                //设置开始时间
                sendCountDtoM.setFromTime(cal.getTime());
            }
            //设置结束时间
            if (StringUtils.isNotBlank(sendCountParamM.getToTime())) {
                sendCountDtoM.setToTime(dateFormat.parse(sendCountParamM.getToTime()+" 23:59:59"));
            }else{
                Calendar calTo = Calendar.getInstance();
                calTo.set(Calendar.HOUR_OF_DAY, 23);
                // 分
                calTo.set(Calendar.MINUTE, 59);
                // 秒
                calTo.set(Calendar.SECOND, 59);
                // 毫秒
                calTo.set(Calendar.MILLISECOND, 0);
                sendCountDtoM.setToTime(calTo.getTime());
            }
        } catch (Exception e) {
            throw new ParamException("传入的日期格式有问题,正确格式为：yyyy-MM-dd HH:mm:ss");
        }

        if (StringUtils.isNotBlank(sendCountParamM.getSpecialParam())) {
            sendCountDtoM.setSpecialParam("%" + sendCountParamM.getSpecialParam() + "%");
        }else{
            sendCountDtoM.setSpecialParam(null);
        }
        return sendCountDtoM;
    }

    //管理员查询工程名称汇总
    public PageResult<SendNameVoM> getSendCountByNameM(SendCountParamM sendCountParamM, PageQuery page) {
        SendCountDtoM sendCountDtoM = getSendCDtoM(sendCountParamM);
        int count = jblSendTruckOUTMapper.getSendCountByNameM(sendCountDtoM);
        if (count > 0){
            List<JbzInfoVo> jbzInfoVos = getjbzInfoM();
            List<SendNameVoM> sendNameVoMList = jblSendTruckOUTMapper.getPageListByNameM(sendCountDtoM, page);
            for (SendNameVoM sendNameVoM:sendNameVoMList) {
                for (JbzInfoVo jbzInfoVo: jbzInfoVos) {
                    if(jbzInfoVo.getCode().equals(sendNameVoM.getUnitid()))
                    {
                        sendNameVoM.setUnitid(jbzInfoVo.getMc());
                        continue;
                    }
                }
                System.out.println("获取单位名称数据"+sendNameVoM);
            }
            return PageResult.<SendNameVoM>builder().total(count).data(sendNameVoMList).build();
        }
        return PageResult.<SendNameVoM>builder().build();
    }

    //管理员查询搅拌站汇总
    public PageResult<SendJbzVoM> getSendCountByJbzM(SendCountParamM sendCountParamM, PageQuery page) {

        SendCountDtoM sendCountDtoM = getSendCDtoMByJbz(sendCountParamM);
        int count = jblSendTruckOUTMapper.getSendCountByJbzM(sendCountDtoM);
        if (count > 0){
            List<JbzInfoVo> jbzInfoVos = getjbzInfoM();
            List<SendJbzVoM> sendJbzVoMList = jblSendTruckOUTMapper.getPageListByJbzM(sendCountDtoM, page);
            for (SendJbzVoM sendJbzVoM:sendJbzVoMList) {
                for (JbzInfoVo jbzInfoVo: jbzInfoVos) {
                    if(jbzInfoVo.getCode().equals(sendJbzVoM.getUnitid()))
                    {
                        sendJbzVoM.setUnitid(jbzInfoVo.getMc());
                        continue;
                    }
                }
                System.out.println("获取单位名称数据"+sendJbzVoM);
            }
            return PageResult.<SendJbzVoM>builder().total(count).data(sendJbzVoMList).build();
        }
        return PageResult.<SendJbzVoM>builder().build();
    }

    //初始化管理员查询参数
    private  SendCountDtoM getSendCDtoMByJbz(SendCountParamM sendCountParamM){
        SendCountDtoM sendCountDtoM = new SendCountDtoM();

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if (StringUtils.isNotBlank(sendCountParamM.getFromTime())) {
                sendCountDtoM.setFromTime(dateFormat.parse(sendCountParamM.getFromTime()));
            }else{
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR_OF_DAY, 0);
                // 分
                cal.set(Calendar.MINUTE, 0);
                // 秒
                cal.set(Calendar.SECOND, 0);
                // 毫秒
                cal.set(Calendar.MILLISECOND, 0);
                cal.add(Calendar.DATE, -7);
                //设置开始时间
                sendCountDtoM.setFromTime(cal.getTime());
            }
            //设置结束时间
            if (StringUtils.isNotBlank(sendCountParamM.getToTime())) {
                sendCountDtoM.setToTime(dateFormat.parse(sendCountParamM.getToTime()));
            }else{
                sendCountDtoM.setToTime(new Date());
            }
        } catch (Exception e) {
            throw new ParamException("传入的日期格式有问题,正确格式为：yyyy-MM-dd");
        }

        if (StringUtils.isNotBlank(sendCountParamM.getSpecialParam())) {
            List<JbzInfoVo> jbzInfoVos = getjbzInfoM();
            for (JbzInfoVo jbzInfoVo: jbzInfoVos) {
                if(jbzInfoVo.getMc().equals(sendCountParamM.getSpecialParam()))
                {
                    sendCountDtoM.setSpecialParam("%" + jbzInfoVo.getCode() + "%");
                    continue;
                }
            }

        }else{
            sendCountDtoM.setSpecialParam(null);
        }
        return sendCountDtoM;
    }

    //获取搅拌站对应列表
    public List<JbzInfoVo> getjbzInfoM(){
        List<JbzInfoVo> jbzInfos = webusercontrolMapper.getjbzInfoM();
        return jbzInfos;
    }
}
